﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using SmallERP.COM;
using SmallERP.MODEL.FoundationModel;
using System.Diagnostics;
using SmallERP.FenPian.Forms;
using SmallERP.FenPian.Presenter;

namespace SmallERP.FenPian
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
                        bool isAppRunning = false;
            System.Threading.Mutex mutex = new System.Threading.Mutex(
                false,
                System.Diagnostics.Process.GetCurrentProcess().ProcessName,
                out isAppRunning);

            if (!isAppRunning)
            {
                return;
            }
            else
            {
                if (DealResult())
                {
                    //Application.EnableVisualStyles();
                    //Application.SetCompatibleTextRenderingDefault(false);
                    //System.Threading.Timer t = new System.Threading.Timer(t_Tick, null, 60000, 100);
                    Application.Run(new FrmFenpian());
                }
            }
        }

        #region 公共的
        private static StartUPUtil _upBLL;
        private static bool isDoing = false;
        static void t_Tick(object sender)
        {
            if (!isDoing)
            {
                isDoing = true;
                if (_upBLL == null)
                {
                    _upBLL = new StartUPUtil();
                }
                //StartUPUtil upBLL = new StartUPUtil();
                ServerInfo serverInfo = new ServerInfo();
                string errMsg = string.Empty;
                int result = _upBLL.GetServerInfo(out serverInfo, out errMsg);
                if (result != 0)
                {
                    MessageBox.Show("与服务器失去连接，程序将立即重启，请检查与服务器的连接！");
                    Environment.Exit(0);
                }
                isDoing = false;
            }
        }

        private static bool DealResult()
        {
            if (_upBLL == null)
            {
                _upBLL = new StartUPUtil();
            }
            //StartUPUtil upBLL = new StartUPUtil();
            //ServerInfo serverInfo = new ServerInfo();
            //string errMsg = string.Empty;
            //int result = _upBLL.GetServerInfo(out serverInfo, out errMsg);
            //if (result != 0)//有问题
            //{
            //    MessageBox.Show(errMsg);

            //    if (result == -6)
            //    {
            //        Application.ApplicationExit += new EventHandler(Restart);
            //    }

            //    Application.Exit();
            //    return false;
            //}

            ////Properties.Settings.Default.ServerIP = serverInfo.ServerIP;
            ////Properties.Settings.Default.ServerPort = serverInfo.ServerTCPPort;
            //return true;
            string ip = _upBLL.GetIpAdress();
            if (string.IsNullOrEmpty(ip))
            {
                FrmAdress frmad = new FrmAdress();
                if (DialogResult.OK == frmad.ShowDialog())
                {
                    ip = frmad.IP;

                }
                frmad.Close();
            }
            DataTransfer.IPadress = ip;
            int modefy = MondifyConfig.Modify(Application.ExecutablePath, ip);
            if (modefy == 0)
            {
               // MessageBox.Show("生成图片地址已改变，请重新启动分片");
                Application.Restart();
                return false;
            }
            else if (modefy == 1)
            {
                return true;
            }
            else
            {
                MessageBox.Show("未修改成功，请重新启动分片");
                //Environment.Exit(1);
                return true;
            }
        }

        private static void Restart(object sender, EventArgs e)
        {
            Process.Start(Application.ExecutablePath);
        }
        #endregion
    }
}
